package com.ruoyi.errorLib.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.service.ISysStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.errorLib.domain.ErrorQuestion;
import com.ruoyi.errorLib.service.IErrorQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 错题本Controller
 */
@Controller
@RequestMapping("/errorLib/question")
public class ErrorQuestionController extends BaseController {
    @Autowired
    private IErrorQuestionService errorQuestionService;
    @Autowired
    private ISysStudentService studentService;
    private String prefix = "jxsx/errorLib";

    @GetMapping()
    public String errorLib(ModelMap mmap)
    {
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("userRole", user.getRoles().get(0).getRoleKey().toString());
        return prefix + "/errorQuestion";
    }
    /**
     * 查询错题列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ErrorQuestion errorQuestion) {
        startPage();
        SysStudent sysStudent=new SysStudent();
        sysStudent.setUserId(ShiroUtils.getUserId()+"");
        SysStudent ss=studentService.selectSysStudentList(sysStudent).stream().findFirst().get();
        String studenId=null;
        if(ss!=null){
            studenId=ss.getStudentId();
        }
        errorQuestion.setStudentId(studenId);
        List<ErrorQuestion> list = errorQuestionService.selectErrorQuestionList(errorQuestion);
        return getDataTable(list);
    }

    /**
     * 导出错题列表
     */
    @Log(title = "错题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErrorQuestion errorQuestion) {
        List<ErrorQuestion> list = errorQuestionService.selectErrorQuestionList(errorQuestion);
        ExcelUtil<ErrorQuestion> util = new ExcelUtil<ErrorQuestion>(ErrorQuestion.class);
        util.exportExcel(response, list, "错题数据");
    }

    /**
     * 获取错题详细信息
     */
    @GetMapping(value = "/detail/{errorId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("errorId") String errorId) {
        return success(errorQuestionService.selectErrorQuestionById(errorId));
    }
    @GetMapping("/detailInit/{errorId}")
    public String errorLibDetail(@PathVariable("errorId") String errorId,ModelMap mmap)
    {
        mmap.put("errorId", errorId);
        return prefix + "/errorQuestionDetail";
    }

//    /**
//     * 新增错题
//     */
//    @Log(title = "错题", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody ErrorQuestion errorQuestion) {
//        return toAjax(errorQuestionService.insertErrorQuestion(errorQuestion));
//    }

    /**
     * 修改错题
     */
    @Log(title = "错题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErrorQuestion errorQuestion) {
        return toAjax(errorQuestionService.updateErrorQuestion(errorQuestion));
    }

    /**
     * 删除错题
     */
    @Log(title = "错题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{errorIds}")
    public AjaxResult remove(@PathVariable String[] errorIds) {
        return toAjax(errorQuestionService.deleteErrorQuestionByIds(errorIds));
    }

    /**
     * 根据学生ID查询错题列表
     */
    @GetMapping("/studentId")
    public AjaxResult getErrorQuestionsByStudentId() {
        SysStudent sysStudent=new SysStudent();
        sysStudent.setUserId(ShiroUtils.getUserId()+"");
        SysStudent ss=studentService.selectSysStudentList(sysStudent).stream().findFirst().get();
        String studenId=null;
        if(ss!=null){
            studenId=ss.getStudentId();
        }
        return success(errorQuestionService.selectErrorQuestionByStudentId(studenId));
    }
} 